﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LTQT.Models;
using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;

namespace LTQT.Pages
{
    public class IndexModel : PageModel
    {
        private readonly LTQT.Data.LTQTContext _context;

        public IndexModel(LTQT.Data.LTQTContext context)
        {
            _context = context;
        }

        //登录
        [BindProperty]
        public string Account { get; set; }
        [BindProperty]
        public string Password { get; set; }

        //注册
        [BindProperty]
        public string Account_New { get; set; }
        [BindProperty]
        public string Password1_New { get; set; }
        [BindProperty]
        public string Password2_New { get; set; }
        [BindProperty]
        public string Name_New { get; set; }
        [BindProperty]
        public string Student_ID_New { get; set; }
        [BindProperty]
        public string WeiXinID_New { get; set; }
        [BindProperty]
        public string PhoneID_New { get; set; }


        public new User User { get; set; }

        public IActionResult OnGet()
        {
            if (HttpContext.User.Identity.IsAuthenticated)
            {
                HttpContext.AuthenticateAsync();
                HttpContext.Session.SetString("USER_NAME", HttpContext.User.Claims.First().Value);
                return LocalRedirect("~/Choose");
            }
            return Page();
        }

        public async Task<IActionResult> OnPostRAsync()
        {
            foreach (var item in _context.User.ToList())
            {
                if (item.Student_ID == Student_ID_New || item.Account == Account_New || item.Name == Name_New)
                {
                    return RedirectToPage("./Index");
                }
            }
            User user = new User
            {
                Account = Account_New,
                Password = Password1_New,
                Name = Name_New,
                Student_ID = Student_ID_New,
                WeiXinID = WeiXinID_New,
                PhoneID = PhoneID_New,
                IsAdmin = 0
            };
            _context.User.Add(user);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }

        public async Task<IActionResult> OnPostLAsync()
        {
            if (!ModelState.IsValid || _context.User.FirstOrDefault(m => m.Account == Account) == null)
            {
                return Page();
            }

            if (_context.User.FirstOrDefault(m => m.Account == Account).Password == Password)
            {
                User = _context.User.FirstOrDefault(m => m.Account == Account);
                await _context.SaveChangesAsync();
                HttpContext.Session.SetString("USER_NAME", User.Name);
                var claims = new List<Claim>
                        {
                            new Claim("LTQTCookie", User.Name),
                        };
                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                ClaimsPrincipal user = new ClaimsPrincipal(claimsIdentity);

                Task.Run(async () =>
                {
                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        user,
                        new AuthenticationProperties()
                        {
                            AllowRefresh = true,
                            IsPersistent = true,
                            ExpiresUtc = DateTimeOffset.UtcNow.AddDays(30),
                        });
                }).Wait();
                return RedirectToPage("./Choose");
            }else
            {
                return RedirectToPage("./Index");
            }

        }

    }
}
